/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Persistencia;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import LogicaNegocio.Cliente;
import LogicaNegocio.OrdenDeReparacion;
import LogicaNegocio.Turno;
import Persistencia.exceptions.NonexistentEntityException;
import Persistencia.exceptions.PreexistingEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author matii_000
 */
public class OrdenDeReparacionJpaController implements Serializable {

      public OrdenDeReparacionJpaController(EntityManagerFactory emf) {
            this.emf = emf;
      }
      private EntityManagerFactory emf = null;

      public EntityManager getEntityManager() {
            return emf.createEntityManager();
      }
public OrdenDeReparacionJpaController(){
          emf=Persistence.createEntityManagerFactory("TrabajoFinalAvanzadaPU");
    }
      public void create(OrdenDeReparacion ordenDeReparacion)throws PreexistingEntityException, Exception {
            EntityManager em = null;
            try {
                  em = getEntityManager();
                  em.getTransaction().begin();
                  Cliente unCliente = ordenDeReparacion.getUnCliente();
                  if (unCliente != null) {
                        unCliente = em.getReference(unCliente.getClass(), unCliente.getCodigo());
                        ordenDeReparacion.setUnCliente(unCliente);
                  }
                  Turno unTurno = ordenDeReparacion.getUnTurno();
                  if (unTurno != null) {
                        unTurno = em.getReference(unTurno.getClass(), unTurno.getCodigo());
                        ordenDeReparacion.setUnTurno(unTurno);
                  }
                  em.persist(ordenDeReparacion);
                  if (unCliente != null) {
                        unCliente.getOrdenesDeReparacion().add(ordenDeReparacion);
                        unCliente = em.merge(unCliente);
                  }
                  if (unTurno != null) {
                        OrdenDeReparacion oldUnaOrdenOfUnTurno = unTurno.getUnaOrden();
                        if (oldUnaOrdenOfUnTurno != null) {
                              oldUnaOrdenOfUnTurno.setUnTurno(null);
                              oldUnaOrdenOfUnTurno = em.merge(oldUnaOrdenOfUnTurno);
                        }
                        unTurno.setUnaOrden(ordenDeReparacion);
                        unTurno = em.merge(unTurno);
                  }
                  em.getTransaction().commit();
            } finally {
                  if (em != null) {
                        em.close();
                  }
            }
      }

      public void edit(OrdenDeReparacion ordenDeReparacion) throws NonexistentEntityException, Exception {
            EntityManager em = null;
            try {
                  em = getEntityManager();
                  em.getTransaction().begin();
                  OrdenDeReparacion persistentOrdenDeReparacion = em.find(OrdenDeReparacion.class, ordenDeReparacion.getCodigo());
                  Cliente unClienteOld = persistentOrdenDeReparacion.getUnCliente();
                  Cliente unClienteNew = ordenDeReparacion.getUnCliente();
                  Turno unTurnoOld = persistentOrdenDeReparacion.getUnTurno();
                  Turno unTurnoNew = ordenDeReparacion.getUnTurno();
                  if (unClienteNew != null) {
                        unClienteNew = em.getReference(unClienteNew.getClass(), unClienteNew.getCodigo());
                        ordenDeReparacion.setUnCliente(unClienteNew);
                  }
                  if (unTurnoNew != null) {
                        unTurnoNew = em.getReference(unTurnoNew.getClass(), unTurnoNew.getCodigo());
                        ordenDeReparacion.setUnTurno(unTurnoNew);
                  }
                  ordenDeReparacion = em.merge(ordenDeReparacion);
                  if (unClienteOld != null && !unClienteOld.equals(unClienteNew)) {
                        unClienteOld.getOrdenesDeReparacion().remove(ordenDeReparacion);
                        unClienteOld = em.merge(unClienteOld);
                  }
                  if (unClienteNew != null && !unClienteNew.equals(unClienteOld)) {
                        unClienteNew.getOrdenesDeReparacion().add(ordenDeReparacion);
                        unClienteNew = em.merge(unClienteNew);
                  }
                  if (unTurnoOld != null && !unTurnoOld.equals(unTurnoNew)) {
                        unTurnoOld.setUnaOrden(null);
                        unTurnoOld = em.merge(unTurnoOld);
                  }
                  if (unTurnoNew != null && !unTurnoNew.equals(unTurnoOld)) {
                        OrdenDeReparacion oldUnaOrdenOfUnTurno = unTurnoNew.getUnaOrden();
                        if (oldUnaOrdenOfUnTurno != null) {
                              oldUnaOrdenOfUnTurno.setUnTurno(null);
                              oldUnaOrdenOfUnTurno = em.merge(oldUnaOrdenOfUnTurno);
                        }
                        unTurnoNew.setUnaOrden(ordenDeReparacion);
                        unTurnoNew = em.merge(unTurnoNew);
                  }
                  em.getTransaction().commit();
            } catch (Exception ex) {
                  String msg = ex.getLocalizedMessage();
                  if (msg == null || msg.length() == 0) {
                        int id = ordenDeReparacion.getCodigo();
                        if (findOrdenDeReparacion(id) == null) {
                              throw new NonexistentEntityException("The ordenDeReparacion with id " + id + " no longer exists.");
                        }
                  }
                  throw ex;
            } finally {
                  if (em != null) {
                        em.close();
                  }
            }
      }

      public void destroy(int id) throws NonexistentEntityException {
            EntityManager em = null;
            try {
                  em = getEntityManager();
                  em.getTransaction().begin();
                  OrdenDeReparacion ordenDeReparacion;
                  try {
                        ordenDeReparacion = em.getReference(OrdenDeReparacion.class, id);
                        ordenDeReparacion.getCodigo();
                  } catch (EntityNotFoundException enfe) {
                        throw new NonexistentEntityException("The ordenDeReparacion with id " + id + " no longer exists.", enfe);
                  }
                  Cliente unCliente = ordenDeReparacion.getUnCliente();
                  if (unCliente != null) {
                        unCliente.getOrdenesDeReparacion().remove(ordenDeReparacion);
                        unCliente = em.merge(unCliente);
                  }
                  Turno unTurno = ordenDeReparacion.getUnTurno();
                  if (unTurno != null) {
                        unTurno.setUnaOrden(null);
                        unTurno = em.merge(unTurno);
                  }
                  em.remove(ordenDeReparacion);
                  em.getTransaction().commit();
            } finally {
                  if (em != null) {
                        em.close();
                  }
            }
      }

      public List<OrdenDeReparacion> findOrdenDeReparacionEntities() {
            return findOrdenDeReparacionEntities(true, -1, -1);
      }

      public List<OrdenDeReparacion> findOrdenDeReparacionEntities(int maxResults, int firstResult) {
            return findOrdenDeReparacionEntities(false, maxResults, firstResult);
      }

      private List<OrdenDeReparacion> findOrdenDeReparacionEntities(boolean all, int maxResults, int firstResult) {
            EntityManager em = getEntityManager();
            try {
                  CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
                  cq.select(cq.from(OrdenDeReparacion.class));
                  Query q = em.createQuery(cq);
                  if (!all) {
                        q.setMaxResults(maxResults);
                        q.setFirstResult(firstResult);
                  }
                  return q.getResultList();
            } finally {
                  em.close();
            }
      }

      public OrdenDeReparacion findOrdenDeReparacion(int id) {
            EntityManager em = getEntityManager();
            try {
                  return em.find(OrdenDeReparacion.class, id);
            } finally {
                  em.close();
            }
      }

      public int getOrdenDeReparacionCount() {
            EntityManager em = getEntityManager();
            try {
                  CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
                  Root<OrdenDeReparacion> rt = cq.from(OrdenDeReparacion.class);
                  cq.select(em.getCriteriaBuilder().count(rt));
                  Query q = em.createQuery(cq);
                  return ((Long) q.getSingleResult()).intValue();
            } finally {
                  em.close();
            }
      }
      
}
